<template>
  <el-container v-loading="loading">
    <div class="box">
      <h3>{{activeheader}}</h3>
      <div class="input-box">
        <span></span>
        <label for="" class="label require">机构名称：</label>
        <el-input class="input-width" v-model="name" placeholder="请输入内容"></el-input>
      </div>
      <div class="input-box">
        <span></span>
        <label for="" class="label require contact">联系人：</label>
        <el-input class="input-width" v-model="contactPerson" placeholder="请输入内容"></el-input>
      </div>
      <div class="input-box">
        <span></span>
        <label for="" class="label require">联系电话：</label>
        <el-input class="input-width" v-model="phone" placeholder="请输入内容"></el-input>
      </div>
      <div class="input-box box-last">
        <el-button type="primary" @click="submit">提交</el-button>
        <el-button @click="cancle">取消</el-button>
      </div>
    </div>
  </el-container>
</template>

<script>
import check from 'utils/check'
let headers = ['新增体检机构','新增中医机构','新增齿科机构'];
let editHeaders = ['修改体检机构','修改中医机构','修改齿科机构'];
let requireArray = ['name','contactPerson','phone'];
let urls = ['/physical','/chinaMed','/dentistry'];
export default {
  name: 'addOrg',
  data() {
    return {
      name: '',
      contactPerson: '',
      phone: '',
      loading: false,
      check: check.methods.check,
      checkTip: check.tips.checkTip,
      activeheader: headers[0],
      code: '0',  //跳转机构类型
      id: '' //编辑id 修改
    }
  },
  mounted(){
    this.editInit();
  },
  methods:{
    editInit(){
      if( !this.$route.query ) return;      
      let { type, code, id } = this.$route.query;
      this.code = code;
      this.id = id;
      let headerArry = (type === 'edit') ? editHeaders : headers;
      this.activeheader = headerArry[this.code];
      if( !id ) return;

      this.loading = true;
      let url = urls[this.code];
      this.api.get(`${url}/${id}`).then((res) => { //查询产品详情
        this.dataInit(res.data)
        this.loading = false;
      }).catch((err) => {
        console.log(err)
        this.loading = false;
        this.$message(err.data.message)
      })
    },
    dataInit(data){
      data = data || {};
      let { id, name, phone, contactPerson } = data;
      this.id = id;
      this.name = name;
      this.phone = phone;
      this.contactPerson = contactPerson;
    },
    submit(){
      let param = {
        name: this.name,
        contactPerson: this.contactPerson,
        phone: this.phone
      }
      this.check(param, requireArray).then(()=>{ 
        this.addUser(param) 
      },()=>{ 
        this.$message(this.checkTip) 
      }).catch((error)=>{ 
        this.$message(error) 
      });
    },
    editUser(param){ //修改用户
      param.id = this.id;
      this.api.put(urls[this.code], param, 'form').then((res)=>{
        this.$router.go(-1);
      }).catch((err) => {
        console.log(err)
        this.$message(err.data.message)
      })
    },
    addUser(param){
      if(this.$route.query.type === 'edit') return this.editUser(param);
      this.api.post(urls[this.code], param, 'form').then((res)=>{
        this.$router.go(-1);
      }).catch((err) => {
        this.$message(err.data.message)
      })
    },
    cancle(){
        this.$router.go(-1)
    }
  }
}
</script>
 
<style lang="less" scoped>
.box{
  padding-left: 20px;
  h3{
    margin-top: 10px;
    margin-bottom: 20px;
  }
}
.input-box {
  display: flex;
  align-items: center;
  margin-bottom: 15px;
  margin-left: 10px;
  &.box-last{
    margin-left: 30px;
    margin-top: 20px;
  }
}
.label{
  width: 80px;
  min-width: 80px;
  margin-left: 20px;
  margin-right: 10px;
  text-align: right;
  &.contact{
    text-indent: 1em;
  }
  &.require{
    position: relative;
    &::before{
        content: '*';
        color: red;
        position: absolute;
        left: -10px;
        top: 4px;
    }  
  }
}
.label-left{
  margin-left: 15px;
}
.input-width{
  width: 216px;
}
.el-textarea{
  width:300px;
}
.el-select{
  margin-right: 10px;
}
.avatar-uploader{
  border: 1px dashed #d9d9d9;
  border-radius: 6px;
}
.avatar-uploader .el-upload {
    border: 1px dashed #d9d9d9;
    border-radius: 6px;
    cursor: pointer;
    position: relative;
    overflow: hidden;
  }
  .avatar-uploader .el-upload:hover {
    border-color: #409EFF;
  }
  .avatar-uploader-icon {
    font-size: 28px;
    color: #8c939d;
    width: 178px;
    height: 178px;
    line-height: 178px;
    text-align: center;
  }
  .avatar {
    width: 178px;
    height: 178px;
    display: block;
  }
</style>
 